package controller

import (
	"database/sql"
	"fmt"
	"mbti/model"

	"github.com/gin-gonic/gin"
)

type Student struct {
	Id            string `json:"id,omitempty"`
	Student_id    string `json:"student_id,omitempty"`
	Student_name  string `json:"student_name,omitempty"`
	College       string `json:"college,omitempty"`
	Specialize    string `json:"specialize,omitempty"`
	Student_class string `json:"student_class,omitempty"`
}

//获取所有学生 offset 为偏移量
func GetStudent(ctx *gin.Context) {
	offset := ctx.Query("offset")
	student_id := ctx.Query("student_id")
	fmt.Println("student_id::::", student_id)
	var result *sql.Rows
	var err error
	if len(student_id) > 0 {
		result, err = model.DB.Query(fmt.Sprintf("select * from student where student_id = %s", student_id))
	} else {
		result, err = model.DB.Query(fmt.Sprintf("select * from student limit 10 offset %s", offset))
	}

	if err != nil {
		fmt.Println("GetExistUser err:", err)
	}

	var stu Student
	var allStu []Student
	//var moment string
	for result.Next() {

		err = result.Scan(&stu.Id, &stu.Student_id, &stu.Student_name, &stu.College, &stu.Specialize, &stu.Student_class)
		allStu = append(allStu, stu)
	}
	fmt.Println(allStu)
	fmt.Println(allStu)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  allStu,
	})
}

// 删除学生
func DeleteStudent(ctx *gin.Context) {
	id := ctx.PostForm("id")

	stmt, err := model.DB.Prepare("delete from student where id=$1")
	if err != nil {
		fmt.Println("Some amazing wrong happens in preparation for the insert.")
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "删除失败",
		})
		return
	}
	defer stmt.Close()
	res, err := stmt.Exec(id)
	if err != nil {
		fmt.Println("Some amazing wrong happens in the process of doInsertOne.")
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "删除失败",
		})
		return
	}
	num, err := res.RowsAffected()
	if err != nil {
		fmt.Println("Some amazing wrong happens in the affected fo deletion.")
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "删除失败",
		})
		return
	}
	fmt.Println("Successfully add ", num)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}

//修改学生
func UpdateStudent(ctx *gin.Context) {
	class := ctx.PostForm("studentClass")
	specialize := ctx.PostForm("specialize")
	college := ctx.PostForm("college")
	name := ctx.PostForm("studentName")
	studentID := ctx.PostForm("studentID")
	id := ctx.PostForm("id")

	stmt, err := model.DB.Prepare("update student set student_name= $1,college=$2,specialize=$3,student_class=$4,student_id=$5 where id=$6")
	if err != nil {
		fmt.Println("Some amazing wrong happens in preparation for the insert.")
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "更新失败,该student_id已存在",
		})
		return
	}
	defer stmt.Close()
	res, err := stmt.Exec(class, specialize, college, name, studentID, id)
	if err != nil {
		fmt.Println("Some amazing wrong happens in the process of doInsertOne.", err)
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "更新失败,该student_id已存在或数据有误",
		})
		return
	}
	num, err := res.RowsAffected()
	if err != nil {
		fmt.Println("Some amazing wrong happens in the affected fo deletion.")
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "更新失败",
		})
		return
	}
	fmt.Println("Successfully add ", num)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "更新成功",
	})
	return
}
